package com.study.ssm.aop.proxy.cglib;

import com.study.ssm.factory.bean.User;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author zhoujunlin
 * @date 2023年07月24日 10:11
 * @desc
 */
public class UserServiceCGLIBProxy {

    public static void main(String[] args) {

        Enhancer enhancer = new Enhancer();
        enhancer.setClassLoader(UserServiceCGLIBProxy.class.getClassLoader());
        // 普通对象  不实现接口
        UserServiceImpl userServiceImpl = new UserServiceImpl();
        enhancer.setSuperclass(userServiceImpl.getClass());

        MethodInterceptor methodInterceptor = new MethodInterceptor() {
            @Override
            public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                System.out.println("=================before cglib log==============");
                return method.invoke(userServiceImpl, args);
            }
        };
        enhancer.setCallback(methodInterceptor);

        UserServiceImpl userService = (UserServiceImpl) enhancer.create();
        userService.register(new User());
        userService.query("zhoujl", "123456");

    }


}
